package com.simon.mctm.biz.sys.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.simon.mctm.base.response.DoResponse;
import com.simon.mctm.biz.sys.bean.Permission;
import com.simon.mctm.biz.sys.dao.PermissionMapper;
import com.simon.mctm.biz.sys.dto.PermissionPcodeDto;
import com.simon.mctm.biz.sys.service.PermissionService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.simon.mctm.exception.OperationFailedException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 菜单&权限 服务实现类
 * </p>
 *
 * @author simon
 * @since 2018-06-15
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements PermissionService {

    @Autowired
    private PermissionMapper permissionMapper;

    /**
     * 根据用户Id获取对应的菜单和权限列表
     *
     * @param userId 用户Id
     * @return 返回菜单和权限列表
     */
    @Override
    public List<Permission> findByUserId(Integer userId) {
        List<Permission> permissionList = permissionMapper.findByUserId(userId);
        PermissionPcodeDto permissionPcodeDto;
        for (Permission permission : permissionList) {
            permissionPcodeDto = getPcodes(permission);
            permission.setPcodes(permissionPcodeDto.getPcodes());
            permission.setLevels(permissionPcodeDto.getLevels());
        }

        return permissionList;
    }

    /**
     * 根据角色Id获取对应的菜单和权限列表
     *
     * @param roleId 角色Id
     * @return 返回菜单和权限列表
     */
    @Override
    public List<Permission> findByRoleId(Integer roleId) {
        List<Permission> permissionList = permissionMapper.findByRoleId(roleId);
        PermissionPcodeDto permissionPcodeDto;
        for (Permission permission : permissionList) {
            permissionPcodeDto = getPcodes(permission);
            permission.setPcodes(permissionPcodeDto.getPcodes());
            permission.setLevels(permissionPcodeDto.getLevels());
        }
        return permissionList;
    }

    /**
     * 根据Id获取菜单&权限
     *
     * @param id id
     * @return 菜单&权限对象
     */
    @Override
    public Permission getPermissionById(Integer id) {
        Permission permission = permissionMapper.selectById(id);
        PermissionPcodeDto permissionPcodeDto;
        permissionPcodeDto = getPcodes(permission);
        permission.setPcodes(permissionPcodeDto.getPcodes());
        permission.setLevels(permissionPcodeDto.getLevels());
        return permission;
    }

    /**
     * 获取菜单&权限列表
     *
     * @return 菜单&权限列表对象
     */
    @Override
    public List<Permission> getPermissionList() {
        EntityWrapper<Permission> ew = new EntityWrapper<>();
        List<Permission> permissionList = permissionMapper.selectList(ew);
        PermissionPcodeDto permissionPcodeDto;
        for (Permission permission : permissionList) {
            permissionPcodeDto = getPcodes(permission);
            permission.setPcodes(permissionPcodeDto.getPcodes());
            permission.setLevels(permissionPcodeDto.getLevels());
        }
        return permissionList;
    }


    /**
     * 获取菜单的父级节点，按[system,user]组成
     * 获取菜单的层级，顶级为1级
     * @param permisson
     * @return
     */
    PermissionPcodeDto getPcodes(Permission permisson) {
        StringBuilder sbPcodes = new StringBuilder("");
        getPcodesByStep(permisson, sbPcodes);
        //存在父节点
        if (sbPcodes.length() > 0) {
            sbPcodes.deleteCharAt(sbPcodes.length()-1);

            String[] arr = sbPcodes.toString().split(",");
            Collections.reverse(Arrays.asList(arr));
            return new PermissionPcodeDto(Arrays.toString(arr),arr.length + 1);
        }

        return new PermissionPcodeDto(null,1);
    }

    /**
     * 递归调用-获取父级节点
     * @param permission 当前节点
     * @param sbPcodes 拼接字符串
     */
    void getPcodesByStep(Permission permission,StringBuilder sbPcodes) {
        //获取当前节点的pcode
        String pcode = permission.getPcode();

        if(pcode!=null && !"".equals(pcode)) {
            sbPcodes.append(pcode).append(",");
            //根据当前节点的pcode获取父节点
            Permission permission1 = new Permission();
            permission1.setCode(pcode);
            Permission menu = permissionMapper.selectOne(permission1);

            getPcodesByStep(menu,sbPcodes);
        }

    }


}
